import org.newdawn.slick.*;
import org.newdawn.slick.tiled.TiledMap;

public class EntityPlayer extends Entity {
	
	private Animation currAnim;
	private int state;
	private boolean inGrid;
	
	private static String IMAGE = "content/bunny.png";
	
	// number of tiles in height/width, set to random number for now
	private static final int DIMENSIONS_IN_TILES = 30;
	
	Animation upAnim, rightAnim, downAnim, idleItchAnim, leftAnim, idleAnim, carrotAnim, exitAnim, idleUp, idleLeft, idleRight;
	
	public EntityPlayer(int x, int y) {
		super(IMAGE, x, y);
		idleUp = new Animation(sheet, 0, 1, 0, 1, true, 100, true);
		idleLeft = new Animation(sheet, 0, 2, 0, 2, true, 100, true);
		idleRight = new Animation(sheet, 0, 3, 0, 3, true, 100, true);
		idleAnim = new Animation(sheet, 0, 0, 0, 0, true, 100, true);
		idleItchAnim = new Animation(sheet, 0, 0, 12, 0, true, 100, true);
		upAnim = new Animation(sheet, 3, 4, 7, 4, true, 100, true);
		rightAnim = new Animation(sheet, 2, 7, 6, 7, true, 80, true);
		downAnim = new Animation(sheet, 3, 6, 6, 6, true, 100, true);
		leftAnim = new Animation(sheet, 2, 5, 6, 5, true, 80, true);
		carrotAnim = new Animation(sheet, 0, 8, 2, 8, true, 250, true);
		exitAnim = new Animation(sheet, 0, 0, 12, 0, true, 100, true);
		currAnim = idleAnim;
		
		itchTimer = 0;
	}
	
	public void setPos(int x, int y) {
		posX = x;
		posY = y;
	}
	
	public int getX() {
		return posX;
	}
	public int getY() {
		return posY;
	}
	
	//Mostly for arrow keys
	public void move(int d) {
		state = d;
		if (d == BunnyAction.NULL || d == BunnyAction.UP || d == BunnyAction.RIGHT || d == BunnyAction.DOWN || 
				d == BunnyAction.LEFT || d == BunnyAction.EXIT || d == BunnyAction.CARROT) {
			state = d;
		} else {
			state = BunnyAction.NULL;
		}
	}
	
	//Movement based on a call's volume (one direction)
	public void move(int direction, int volume, int Xdest, int Ydest) {
		int spaces = 5;
		AStar mover = new AStar();
		int  aMove = mover.getAction(posX, posY, Xdest, Ydest);
		if (aMove == direction) {
			for (int i = 0; i < 5; i++) {
				move(mover.getAction(posX, posY, Xdest, Ydest));
			}		
		} else {
			for (int i = 0; i < spaces; i++) {
				move(direction);
			}
		}
	}
	
	private int itchTimer;
	public void update() {
		inGrid = posX * Map.TILE_SIZE == actualX && posY * Map.TILE_SIZE == actualY;
		
		if(inGrid) {
			if(state == BunnyAction.LEFT) {
				if(posX - 1 >= 0 && posX - 1 < DIMENSIONS_IN_TILES) {
					if(Map.tiles[posY][posX - 1] != Tiles.SOLID) {
						posX--;
						state = BunnyAction.NULL;
					}
				}
			} else if(state == BunnyAction.RIGHT) {
				if(posX + 1 >= 0 && posX + 1 < DIMENSIONS_IN_TILES) {
					if(Map.tiles[posY][posX + 1] != Tiles.SOLID) {
						posX++;
						state = BunnyAction.NULL;
					}
				}
			} else if(state == BunnyAction.UP) {
				if(posY - 1 >= 0 && posY - 1 < DIMENSIONS_IN_TILES) {
					if(Map.tiles[posY - 1][posX] != Tiles.SOLID) {
						posY--;
						state = BunnyAction.NULL;
					}
				}
			} else if(state == BunnyAction.DOWN) {
				if(posY + 1 >= 0 && posY + 1 < DIMENSIONS_IN_TILES) {
					if(Map.tiles[posY + 1][posX] != Tiles.SOLID) {
						posY++;
						state = BunnyAction.NULL;
					}
				}
			}
		}
		
		updatePositionSmooth();
	}
	
	public void render() {
		
//		if(state == BunnyAction.LEFT) {
//			currAnim = leftAnim;
//		} else if(state == BunnyAction.RIGHT) {
//			currAnim = rightAnim;
//		} else if(state == BunnyAction.UP) {
//			currAnim = upAnim;
//		} else if(state == BunnyAction.DOWN) {
//			currAnim = downAnim;
//		} else if(state == BunnyAction.CARROT) {
//			currAnim = carrotAnim;
//		} else if(state == BunnyAction.EXIT) {
//			currAnim = exitAnim;
//		}
		if(actualX > posX * Map.TILE_SIZE) {
			currAnim = leftAnim;
		} else if(actualX < posX * Map.TILE_SIZE) {
			currAnim = rightAnim;
		}
		if(actualY > posY * Map.TILE_SIZE) {
			currAnim = upAnim;
		} else if(actualY < posY * Map.TILE_SIZE) {
			currAnim = downAnim;
		}
		if (inGrid) {
//			if (currAnim != idleLeft && currAnim != idleRight && currAnim != idleUp) {
//				if (currAnim == leftAnim) {
//					currAnim = idleLeft;
//				}
//				else if (currAnim == rightAnim) {
//					currAnim = idleRight;
//				}
//				else if (currAnim == upAnim) {
//					currAnim = idleUp;
//				} else {
//					currAnim = idleAnim;
//				}
//			}
			if(state == BunnyAction.LEFT) {
				currAnim = idleLeft;
			} else if(state == BunnyAction.RIGHT) {
				currAnim = idleRight;
			} else if(state == BunnyAction.UP) {
				currAnim = idleUp;
			} else if(state == BunnyAction.DOWN) {
				currAnim = idleAnim;
			}
			if (currAnim == leftAnim) {
				currAnim = idleLeft;
			} else if (currAnim == rightAnim) {
				currAnim = idleRight;
			} else if (currAnim == upAnim) {
				currAnim = idleUp;
			} else if(currAnim == downAnim) {
				currAnim = idleAnim;
			}
			
			if(currAnim == idleAnim || currAnim == idleItchAnim) {
				if(itchTimer <= 0) {
					currAnim = idleItchAnim;
					if(itchTimer == 0) {
						idleItchAnim.setCurrentFrame(0);
						itchTimer--;
					}
					if(idleItchAnim.getFrame() == idleItchAnim.getFrameCount() - 1) {
						itchTimer = 250;
					}

				} else {
					itchTimer--;
					currAnim = idleAnim;
				}

			}
		}
		
		if(posX == 13 && posY == 10) {
			currAnim = carrotAnim;
			currAnim.stopAt(carrotAnim.getFrameCount() - 1);
		} else {
			carrotAnim.start(); 	
			carrotAnim.setCurrentFrame(0);
		}
		currAnim.draw(actualX, actualY);
	}
}

